package com.nulldev.util.web.YouTubeExtractor.dfs_fix;

import com.nulldev.util.JVM.Arguments;
import com.nulldev.util.VariableAPI.MathUtil.TimeUtil;
import com.nulldev.util.VariableAPI.RandomUtil;
import com.nulldev.util.data.CachedItem;
import com.nulldev.util.logging.Logger;
import com.nulldev.util.logging.LoggerFactory;
import com.nulldev.util.scripting.ScriptAPI.nScripting;
import com.nulldev.util.web.YouTubeExtractor.YouTubeExtractor;
import com.nulldev.util.web.YouTubeExtractor.signatures.ExtractedSignatureData;
import com.nulldev.util.web.YouTubeExtractor.signatures.SignatureExtractor;
import com.nulldev.util.web.YouTubeExtractor.signatures.extractor.V9Extractor;
import com.nulldev.util.web.YouTubeExtractor.signatures.extractors.v9.RhinoBasedV9Extractor;

public class CachedDFSFixer extends CachedItem<V9Extractor> {

	private static final Logger log = LoggerFactory
			.getLoggerD(Arguments.hasArgument("--debug", "-debug", "--trace", "--SignatureUpdates.debug", "--SignatureExtractor.debug"));

	public CachedDFSFixer() {
		super("YTE_DFS", TimeUtil.fromMinutes(60 + RandomUtil.randInt(-5, 5)), false, true);
	}

	@Override
	public V9Extractor accquire() throws Exception {
		log.debug("accquire()");
		YouTubeExtractor.extractor.getAuto();
		final ExtractedSignatureData esd = DFSFix.fromData(null, SignatureExtractor.getCachedPlayerURL());
		log.debug("accquire:esd: " + esd);
		if (nScripting.isRhinoInstalled()) {
			return new RhinoBasedV9Extractor(esd);
		} else {
			throw new UnsupportedOperationException("Rhino is required to handle DFS, (for now) sorry about this.");
		}
	}

}
